package com.kotlin.tsw.usercenter.ui.activity

import android.os.Bundle
import android.view.View
import com.kotlin.base.widgets.VerifyButton
import com.kotlin.tsw.baselibrary.common.AppManager
import com.kotlin.tsw.baselibrary.ext.enable
import com.kotlin.tsw.baselibrary.ext.onClick
import com.kotlin.tsw.baselibrary.ui.activity.BaseMVPActivity
import com.kotlin.tsw.usercenter.R
import com.kotlin.tsw.usercenter.injection.component.DaggerUserComponent
import com.kotlin.tsw.usercenter.injection.module.UserModule
import com.kotlin.tsw.usercenter.presenter.RegisterPersenter
import com.kotlin.tsw.usercenter.presenter.view.RegisterView
import kotlinx.android.synthetic.main.activity_register.*
import org.jetbrains.anko.toast

class RegisterActivity : BaseMVPActivity<RegisterPersenter>(), RegisterView, View.OnClickListener {


    private var pressTime: Long = 0
    override fun injectComponent() {
        DaggerUserComponent.builder().activityComponent(activityComponent).userModule(UserModule()).build().inject(this)
        mPresenter.mView = this
    }

    override fun onRegisterResult(result: String) {
        toast(result)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_register)

        initView()

//        mRegisterBtn.setOnClickListener {
//            mPresenter.register(mMobileEt.text.toString().trim(), mVerifyCodeEt.text.toString().trim(), mPwdEt.text.toString().trim())
//        }
//        mRegisterBtn.onClick {
//            mPresenter.register(mMobileEt.text.toString().trim(), mVerifyCodeEt.text.toString().trim(), mPwdEt.text.toString().trim())
//        }
//        mRegisterBtn.onClick(object : View.OnClickListener {
//            override fun onClick(p0: View?) {
//                mPresenter.register(mMobileEt.text.toString().trim(), mVerifyCodeEt.text.toString().trim(), mPwdEt.text.toString().trim())
//            }
//        })
//        mVerifyCodeBtn.onClick { mVerifyCodeBtn.requestSendVerifyNumber()  }

    }

    private fun initView() {
        mRegisterBtn.enable(mMobileEt,{isButtonEnable()})
        mRegisterBtn.enable(mVerifyCodeEt,{isButtonEnable()})
        mRegisterBtn.enable(mPwdEt,{isButtonEnable()})
        mRegisterBtn.enable(mPwdConfirmEt,{isButtonEnable()})
        mRegisterBtn.setOnClickListener(this)
        mVerifyCodeBtn.setOnClickListener(this)

    }

//    override fun onBackPressed() {
//        val time = System.currentTimeMillis()
//        if (time - pressTime >= 2000) {
//            toast("再按一次返回键退出")
//            pressTime = time
//        } else {
//            AppManager.instant.exitApp(this)
//        }
//
//    }

    override fun onClick(view: View) {
        when (view.id) {
            R.id.mRegisterBtn -> {
                mPresenter.register(mMobileEt.text.toString().trim(), mVerifyCodeEt.text.toString().trim(), mPwdEt.text.toString().trim())
            }
            R.id.mVerifyCodeBtn -> {
                mVerifyCodeBtn.requestSendVerifyNumber()
                toast("验证码已发送")
            }
        }
    }
    fun isButtonEnable():Boolean{
        return !mMobileEt.text.isNullOrEmpty()&&
                !mVerifyCodeEt.text.isNullOrEmpty()&&
                !mPwdEt.text.isNullOrEmpty()&&
                !mPwdConfirmEt.text.isNullOrEmpty()
    }
}
